package com.zzyl.nursing.controller.member;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zzyl.nursing.dto.DeviceDataDto;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.domain.FamilyMemberElder;
import com.zzyl.nursing.dto.FamilyElderDto;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.vo.*;
import com.zzyl.nursing.service.IFamilyMemberElderService;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 老人家属Controller
 *
 * @author syl
 * @date 2025-05-30  member/user/login
 */
@RestController
@RequestMapping("/member/user")
@Api(tags = "老人家属的接口")
public class FamilyMemberController extends BaseController {
    @Autowired
    private IFamilyMemberService familyMemberService;

    @Autowired
    private IFamilyMemberElderService familyMemberElderService;

    /**
     * 登录接口，用于老人家属的登录操作。
     * @param dto 用户登录请求的数据传输对象，包含登录所需信息。
     * @return 返回登录成功的响应结果，包括登录信息。
     */
    @PostMapping("/login")
    @ApiOperation("老人家属的登录")
    public AjaxResult login(@RequestBody UserLoginRequestDto dto) {
        LoginVo loginVo = familyMemberService.login(dto);
        return success(loginVo);
    }

    /**
     * 绑定老人信息。
     * @param dto FamilyElderDto 类型的对象，包含绑定老人所需的信息。
     * @return 返回 AjaxResult 对象，表示操作成功的结果。
     */
    @PostMapping("/add")
    @ApiOperation("绑定老人")
    public AjaxResult add(@RequestBody FamilyElderDto dto) {
        familyMemberElderService.add(dto);
        return success();
    }

    /**
     * 查询绑定老人的数据。
     *
     * @return 返回包含绑定老人信息的AjaxResult对象。
     */
    @GetMapping("/my")
    @ApiOperation("查询绑定老人的数据")
    public AjaxResult findElder() {
        List<ElderReturnInfoVo> list = familyMemberElderService.findElder();
        return success(list);
    }


    /**
     * 查看家人列表（分页查询）。
     *
     * @param pageNum 当前页码，默认为1。
     * @param pageSize 每页显示的记录数，默认为5。
     * @return 返回分页后的家人列表。
     */
    @GetMapping("/list-by-page")
    @ApiOperation("查看家人列表（分页查询）")
    public AjaxResult findListElder(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize) {
       List<ElderlyInfoVO> list = familyMemberElderService.findListElder(pageNum,pageSize);
        return success(list);
    }

    /**
     * 解绑家人。
     * @param id 家人的ID
     * @return 返回操作成功的AjaxResult对象
     */
    @DeleteMapping("/deleteById")
    @ApiOperation("解绑家人")
    public AjaxResult deleteElder(Integer id) {
        FamilyMember familyMember = familyMemberService.getById(id);
        if (familyMember == null) {
            return AjaxResult.error("家人不存在，解绑失败");
        }
        familyMemberService.removeById(id);
        return success();
    }
    @GetMapping("/queryServiceProperties/{iotId}")
    public AjaxResult queryServiceProperties(@PathVariable("iotId") String iotId) {
        List<DeviceDataVo> list = familyMemberService.queryServiceProperties(iotId);
        return AjaxResult.success(list);
    }

    @GetMapping("/queryDeviceDataListByDay")
    public AjaxResult queryDeviceDataListByDay(DeviceDataDto dto) {
        List<DeviceDataVo> list = familyMemberService.queryDeviceDataListByDay(dto);
        List<DataVo> dataVoList = new ArrayList<>();
        for (DeviceDataVo deviceDataVo : list) {
            DataVo dataVo = new DataVo();
            dataVo.setDateTime(deviceDataVo.getDateTime());
            dataVo.setDataValue(deviceDataVo.getDataValue());
            dataVoList.add(dataVo);
        }
        return AjaxResult.success(dataVoList);
    }

    @GetMapping("/queryDeviceDataListByWeek")
    public AjaxResult queryDeviceDataListByWeek(DeviceDataDto dto) {
        List<DataVo> dataVoList = familyMemberService.queryDeviceDataListByWeek(dto);
        return AjaxResult.success(dataVoList);
    }
}
